
var pageBean = null;
var dataList = {};
var suggestStatus = {"0":"未处理","1":"待解决","2":"已处理"};


$(document).ready(function(e) {
	
	$(".select2").uedSelect({
		width : 250  
	});
	
	$(".select3").uedSelect({
		width : 140  
	});

	search(1);	
	initialSpace();//加载和初始化空间信息选择
	initialSelect();//初始化页面上的选择框数据
	$("#btnCancle").click(closeDialog);
	$("#closeDialog").click(closeDialog);
});

function processDialog(id){	//处理
	var currentData = dataList[id];
	$("input:radio[name=processStatus][value='"+currentData['status']+"']").attr("checked",true);
	$("#processResult").val($.trim(currentData.result));
	$("input:radio[name=processStatus]").removeAttr("disabled"); 
	$("#processResult").removeAttr("disabled"); 
	$('.theme-popover-mask').fadeIn(100);
	$('#clueRemarkForm').slideDown(200);
	$("#btnSaveProcessResult").unbind();
	$("#btnSaveProcessResult").click(function(){
		saveProcess(id);
	});
	
}

function detail(id){//查看
	var currentData = dataList[id];
	$("input:radio[name=processStatus][value='"+currentData['status']+"']").attr("checked",true);
	$("#processResult").val($.trim(currentData.result));	
	$("input:radio[name=processStatus]").attr("disabled",true); 
	$("#processResult").attr("disabled",true); 	
	$('.theme-popover-mask').fadeIn(100);
	$('#clueRemarkForm').slideDown(200);
	$("#btnSaveProcessResult").unbind();
	$("#btnSaveProcessResult").click(function(){
		closeDialog();
	});
}

function closeDialog(){//关闭跟弹出窗口
	$('.theme-popover-mask').fadeOut(100);  
	$('#clueRemarkForm').slideUp(200);  
}

function saveProcess(id){
	var currentData = dataList[id];
	var status = $("input:radio[name=processStatus]:checked").val();
	var result = $.trim($("#processResult").val());
	if(result == ''){
		alert("请输入处理结果。")
		return;
	}

	var requestJson = {"data":{"status":status,"result":result,"id":id}};
	$.ajax({
        url: '/euc/onlineops/suggest/update',
        data:JSON.stringify(requestJson),
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (datas, textStatus, response) {
        	//console.log(JSON.stringify(datas));
        	if(datas.status == 0){
            	search(pageBean.currentPage);
            	closeDialog();
            	alert("处理成功！");
        	}else{
        		alert("处理失败！");
        	}
        },
        error: function () {
        	console.log('/euc/onlineops/suggest/update error');
        }
    });
}

function initialSelect(){//初始化页面上的选择框数据
	
	var spaceSelect = $("#searchSpace");	
	$.each(spaces,function(key,space){	
   		$('<option value="'+ key +'">'+ space.spaceName +'</option>').appendTo($(spaceSelect));
	});
	$(spaceSelect).val(-1).trigger('change');
	
}

function initialSpace(){//初始化空间信息
	try{
		var spaceString = localStorage.getItem('space');
		spaces = $.parseJSON(spaceString);
	}catch(e){
		console.log(e.messge);
	}
	$.ajax({
        url: '/euc/system/space',
        data:'',
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	try{
	        	$.each(result,function(index,data){	
	        		spaces[data.id] = data;  
	   			});
	        	var spaceJson = JSON.stringify(spaces);
	        	localStorage.setItem('space',spaceJson);
        	}catch(e){
        		/*alert(e.message);*/
        	}
        },
        error: function () {
        	/*alert("error");*/
        }  
    });
}


function initailPageNumber(totalPage,currentPage,pageLength){				
	try{
			var midNumber = Math.ceil((pageLength-2)/2);
			var paginList = $(".paginList");
			$(paginList).empty();			
			var prev = '<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>';
			var next = '<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>';
			if( totalPage <= pageLength - 2){
				$(prev).appendTo($(".paginList"));
				for(var i = 1; i<totalPage+1; i++){
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
					if(i == currentPage){
						pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
					}			
					$(pagenItem).appendTo($(".paginList"));
				}
				$(next).appendTo($(".paginList"));
			}else{				
				if(currentPage <= midNumber){
					$(prev).appendTo($(".paginList"));
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
						if(i == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
						}			
						$(pagenItem).appendTo($(".paginList"));
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}if(totalPage - currentPage < midNumber){

					var index = totalPage;
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+index+'</a></li>';
						if(index == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+index+'</a></li>';
						}			
						$(pagenItem).prependTo($(".paginList"));
						index--;
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+ 1 +'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					$(next).appendTo($(".paginList"));
				}else if(totalPage - currentPage >= midNumber && currentPage > midNumber){

					pagenItem = '<li class="paginItem current"><a href="javascript:;">'+currentPage+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					var count = midNumber - 3;
					for(var i = 1; i<=count;i++){
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage-i)+'</a></li>';
						$(pagenItem).prependTo($(".paginList"));
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage+i)+'</a></li>';
						$(pagenItem).appendTo($(".paginList"));
					}
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+1	+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}				
			}
			$(".paginItem").click(function(){
				if($(this).hasClass("more")){
					//alert("more");
					return;
				}else if($(this).find(".pagepre").length > 0){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur > 1){
						search(cur - 1);
					}
				}else if($(this).find(".pagenxt").length > 0 ){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur < totalPage){
						search(cur + 1);
					}
				}else{
					var cur = $(this).find("a").html();
					cur = parseInt(cur);
					search(cur);
				}
			});
	}catch(e){/*alert(e.messge);*/}	
}

function search(currentPage){
    var userAreaId = $("#searchSpace").val();
    var suggeststatus = $("#suggeststatus").val()
	var requestJson = {currentPage:currentPage,data:{"userAreaId":userAreaId,"status":suggeststatus}};
    console.log(JSON.stringify(requestJson));
	$.ajax({
        url: '/euc/onlineops/suggest/query',
        data:JSON.stringify(requestJson),
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	console.log(JSON.stringify(result));
        	pageBean = result.data;
        	$("#dataTable").empty();	        	
        	$.each(result.data.list,function(i,data){
        		var html = '<tr>';
        		html += '<td style="width:30px;"><input rowchecked name="'+data.id+'" type="checkbox" value="" /></td>';
        		html += '<td style="width:160px;">' + $.trim(data.content) + '</td>';
        		html += '<td>' +  $.trim(data.personName) + '</td>';
        		html += '<td>' +  $.trim(data.userTel)+ '</td>';
        		html += '<td>' +  $.trim(data.userArea) + '</td>';
        		html += '<td>' +  $.trim(data.addTime) + '</td>';
        		html += '<td>' +  $.trim(suggestStatus[data.status]) + '</td>';
        		html += '<td style="width:170px;">';
        		if(data.status == '2'){//已处理，只能查看
        			html += '<a href="javascript:detail('+data.id+');" class="tablelink">查看</a>&nbsp;&nbsp;&nbsp;';
        		}else if(data.status == '1'){
        			html += '<a href="javascript:detail('+data.id+');" class="tablelink">查看</a>&nbsp;&nbsp;&nbsp;';
        			html += '<a href="javascript:processDialog('+data.id+');" class="tablelink">处理</a></td>';
        		}else{
        			html += '<a href="javascript:processDialog('+data.id+');" class="tablelink">处理</a></td>';
        		}
        		html += '</td>';        		
        		html += '</tr>';
	        	$("#dataTable").append(html);
	        	dataList[data.id] = data;
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');        	
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        	
        },
        error: function () {
        	console.log("error");
        }  
    });
}

